package com.neusoft.neumooc.dao;

import java.util.List;
import java.util.Map;
import java.util.HashMap;

import org.springframework.stereotype.Repository;

import com.neusoft.core.common.BaseDAO;
import com.neusoft.core.common.exception.DAOException;
import com.neusoft.neumooc.vo.BackContentVO;

@Repository
public class BackContentDAOImpl extends BaseDAO implements BackContentDAO {

    private static final String SQL_GET_CONTENTS_BY_COLUMN_ID = 
        "SELECT contentId, columnId, contentMark, title, description, pic, text, " +
               "file, cusFir, cusSec, cusThir, viewTimes, status, " +
               "createTime, createUser, editTime, editUser " +
        "FROM cw_contents WHERE columnId = :columnId";
        
    private static final String SQL_GET_CONTENT_BY_ID = 
        "SELECT contentId, columnId, contentMark, title, description, pic, text, " +
               "file, cusFir, cusSec, cusThir, viewTimes, status, " +
               "createTime, createUser, editTime, editUser " +
        "FROM cw_contents WHERE contentId = :contentId";
        
    private static final String SQL_ADD_CONTENT = 
        "INSERT INTO cw_contents(contentId, columnId, contentMark, title, description, " +
        "pic, text, file, cusFir, cusSec, cusThir, viewTimes, status, createTime, createUser) " +
        "VALUES (:contentId, :columnId, :contentMark, :title, :description, " +
        ":pic, :text, :file, :cusFir, :cusSec, :cusThir, :viewTimes, :status, :createTime, :createUser)";
        
    private static final String SQL_UPDATE_CONTENT = 
        "UPDATE cw_contents SET contentMark = :contentMark, title = :title, " +
        "description = :description, pic = :pic, text = :text, file = :file, " +
        "cusFir = :cusFir, cusSec = :cusSec, cusThir = :cusThir, " +
        "editTime = :editTime, editUser = :editUser " +
        "WHERE contentId = :contentId";
        
    private static final String SQL_DELETE_CONTENT = 
        "DELETE FROM cw_contents WHERE contentId = :contentId";
        
    private static final String SQL_UPDATE_CONTENT_STATUS = 
        "UPDATE cw_contents SET status = :status WHERE contentId = :contentId";

    private static final String SQL_SEARCH_CONTENTS = 
        "SELECT contentId, columnId, contentMark, title, description, pic, text, " +
               "file, cusFir, cusSec, cusThir, viewTimes, status, " +
               "createTime, createUser, editTime, editUser " +
        "FROM cw_contents WHERE 1=1 " +
        "AND (:columnId IS NULL OR columnId = :columnId) " +
        "AND (:contentMark IS NULL OR contentMark LIKE CONCAT('%', :contentMark, '%')) " +
        "AND (:createUser IS NULL OR createUser = :createUser) " +
        "AND (:editUser IS NULL OR editUser = :editUser) " +
        "AND (:status IS NULL OR status = :status)";

    @Override
    public List<BackContentVO> getContentsByColumnId(String columnId) throws DAOException {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("columnId", columnId);
            List<BackContentVO> contents = this.queryForBeanListByBeanMap(SQL_GET_CONTENTS_BY_COLUMN_ID, BackContentVO.class, params);
            log.debug("查询到的内容列表: " + contents);
            return contents;
        } catch (Exception e) {
            log.error("查询内容列表失败", e);
            throw new DAOException("查询内容列表失败", e);
        }
    }

    @Override
    public BackContentVO getContentById(String contentId) throws DAOException {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("contentId", contentId);
            BackContentVO content = this.queryForBeanByBeanMap(SQL_GET_CONTENT_BY_ID, BackContentVO.class, params);
            log.debug("查询到的内容: " + content);
            return content;
        } catch (Exception e) {
            log.error("查询内容失败", e);
            throw new DAOException("查询内容失败", e);
        }
    }

    @Override
    public boolean addContent(BackContentVO content) throws DAOException {
        try {
            this.update(SQL_ADD_CONTENT, content);
            return true;
        } catch (Exception e) {
            log.error("添加内容失败", e);
            return false;
        }
    }

    @Override
    public boolean updateContent(BackContentVO content) throws DAOException {
        try {
            this.update(SQL_UPDATE_CONTENT, content);
            return true;
        } catch (Exception e) {
            log.error("更新内容失败", e);
            return false;
        }
    }

    @Override
    public boolean deleteContent(String contentId) throws DAOException {
        try {
            BackContentVO content = new BackContentVO();
            content.setContentId(contentId);
            this.update(SQL_DELETE_CONTENT, content);
            return true;
        } catch (Exception e) {
            log.error("删除内容失败", e);
            return false;
        }
    }

    @Override
    public boolean updateContentStatus(String contentId, String status) throws DAOException {
        try {
            BackContentVO content = new BackContentVO();
            content.setContentId(contentId);
            content.setStatus(status);
            log.info(contentId);
            log.info(status);
            
            this.update(SQL_UPDATE_CONTENT_STATUS, content);
            return true;
        } catch (Exception e) {
            log.error("更新内容状态失败", e);
            throw new DAOException("更新内容状态失败", e);
        }
    }

    @Override
    public List<BackContentVO> searchContents(BackContentVO searchParams) throws DAOException {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            
            if(searchParams.getColumnId() != null && !searchParams.getColumnId().trim().isEmpty()) {
                params.put("columnId", searchParams.getColumnId());
            }
            if(searchParams.getContentMark() != null && !searchParams.getContentMark().trim().isEmpty()) {
                params.put("contentMark", searchParams.getContentMark());
            }
            if(searchParams.getCreateUser() != null && !searchParams.getCreateUser().trim().isEmpty()) {
                params.put("createUser", searchParams.getCreateUser());
            }
            if(searchParams.getEditUser() != null && !searchParams.getEditUser().trim().isEmpty()) {
                params.put("editUser", searchParams.getEditUser());
            }
            if(searchParams.getStatus() != null && !searchParams.getStatus().trim().isEmpty()) {
                params.put("status", searchParams.getStatus());
            }
            
            StringBuilder sql = new StringBuilder(
                "SELECT contentId, columnId, contentMark, title, description, pic, text, " +
                "file, cusFir, cusSec, cusThir, viewTimes, status, " +
                "createTime, createUser, editTime, editUser " +
                "FROM cw_contents WHERE 1=1 ");
                
            if(params.containsKey("columnId")) {
                sql.append("AND columnId = :columnId ");
            }
            if(params.containsKey("contentMark")) {
                sql.append("AND contentMark LIKE CONCAT('%', :contentMark, '%') ");
            }
            if(params.containsKey("createUser")) {
                sql.append("AND createUser = :createUser ");
            }
            if(params.containsKey("editUser")) {
                sql.append("AND editUser = :editUser ");
            }
            if(params.containsKey("status")) {
                sql.append("AND status = :status ");
            }
            
            List<BackContentVO> contents = this.queryForBeanListByBeanMap(sql.toString(), BackContentVO.class, params);
            log.debug("搜索到的内容列表: " + contents);
            return contents;
        } catch (Exception e) {
            log.error("搜索内容失败", e);
            throw new DAOException("搜索内容失败", e);
        }
    }
} 